﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class TasksCreateHandler : IRequestHandler<TasksCreateCommon, Result>
    {
        private readonly IBaseRepository<ProductionTasks> ProductionTasksRepository;
        public IMapper mapper { get; set; }
        public TasksCreateHandler(IBaseRepository<ProductionTasks> productionTasksRepository, IMapper mapper)
        {
            ProductionTasksRepository = productionTasksRepository;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(TasksCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<ProductionTasks>(request);
            string sql = "insert into ProductionTasks values(@TasksId,@TasksCode,@TasksName,@TasksType,@TasksState,@ChooseId,@MaterialSpecsId,@ItemUnitId,@ProductionNum,@StartTime,@CompletionTime,@ItemManifestCode,@ItemManifestVersion,@ProcessRouteNo,@ProcessVersion,@InboundQuantity,@NumberReturns,@Peole,@DepartmentId,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNum = ProductionTasksRepository.Add(sql, new
            {
                AddData.TasksId,
                AddData.TasksCode,
                AddData.TasksName,
                AddData.TasksType,
                AddData.TasksState,
                AddData.ChooseId,
                AddData.MaterialSpecsId,
                AddData.ItemUnitId,
                request.ProductionNum,
                AddData.StartTime,
                AddData.CompletionTime,
                AddData.ItemManifestCode,
                AddData.ItemManifestVersion,
                AddData.ProcessRouteNo,
                AddData.ProcessVersion,
                AddData.InboundQuantity,
                AddData.NumberReturns,
                AddData.Peole,
                AddData.DepartmentId,
                
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
                AddData.IsDelete,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
